/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "my_inc.h"


/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
#define  OFF   0
#define  ON    1


/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */



/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

	//uint32_t    light, sound;
	double   temp;
	double   hum;
	uint8_t  tx_buffer[50] = {0};



  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */



  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  SHT20_Init();

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_TIM6_Init();
  MX_USART1_UART_Init();
  MX_ADC1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */

   // 启用中断接收
	HAL_UART_Receive_IT(&huart2, uart2.recv_buffer, uart2_recv_len);

	//发送多条AT命令
	send_at_cmds();


#if 0
	send_at_cmd((uint8_t*)"AT+RST\r\n",(uint8_t*)"OK");                //重启
	HAL_Delay(2000);
	//send_at_cmd((uint8_t*)"ATE0\r\n",(uint8_t*)"OK");
	//send_at_cmd((uint8_t*)"AT+CWMODE=1\r\n",(uint8_t*)"OK");		//查询服务
	send_at_cmd((uint8_t*)"AT+MQTTUSERCFG=0,1,\"NULL\",\"6426d4de0eafe83f11db2777_0001\",\"c5fc49b269aeebd1d8e17197a79b7aa30f4365f837d58891d580497901dbbf85\",0,0,\"\"\r\n",(uint8_t*)"OK");
	send_at_cmd((uint8_t*)"AT+MQTTCLIENTID=0,\"6426d4de0eafe83f11db2777_0001_0_0_2023033113\"\r\n",(uint8_t*)"OK");
	send_at_cmd((uint8_t*)"AT+MQTTCONN=0,\"c601d9bb36.st1.iotda-device.cn-east-3.myhuaweicloud.com\",1883,1\r\n",(uint8_t*)"OK");
	send_at_cmd((uint8_t*)"AT+MQTTSUB=0,\"$oc/devices/6426d4de0eafe83f11db2777_0001/sys/messages/up\",1\r\n",(uint8_t*)"OK");
	send_at_cmd((uint8_t*)"AT+MQTTPUB=0,\"$oc/devices/6426d4de0eafe83f11db2777_0001/sys/messages/up\",\"{\"services\": [{\"service_id\": \"1\",\"properties\": {\"CurrentTemperature\": 27.8}}]}\",0,0\r\n",(uint8_t*)"OK");
	HAL_Delay(7000);
#endif



  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

  //printf("Start ISKBroad Example Program %.2f v1.0\r\n", 39.0);

  while (1)
  {
#if 0
	  adc_sample_light_sound(&light, &sound);
	  printf("Lux[%lu] Sound[%lu]\r\n", light, sound);
	  HAL_Delay(5000);
#endif


    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */


#if 0
	HAL_UART_Receive_IT(&huart2, uart2.rv_buffer, uart2_recv_len);

	if(strstr((const char*)uart2.rv_buffer, "led_b_1"))
	{
		led.led_on(led_b);
	}
	else if(strstr((const char*)uart2.rv_buffer, "led_b_0"))
	{
		led.led_off(led_b);
	}

	else if(strstr((const char*)uart2.rv_buffer, "TH"))
	{
		//HAL_UART_Transmit(&huart2, tx_buffer, strlen((char *)tx_buffer), 60);
		led.led_on(led_r);
		//HAL_Delay(4000);
	}

	//HAL_Delay()
#endif

#if 0
	printf("*******start send TH!*******\r\n");

	temp = SHT20_Get_T_Value(SHT20_ReadData(SHT20_T_ADDR));
	hum = SHT20_Get_T_Value(SHT20_ReadData(SHT20_RH_ADDR));
	//printf("tx_buffer: %s\r\n", tx_buffer);

	snprintf((char *)tx_buffer, sizeof(tx_buffer), "Temperature:%.2f Humidity:%.2f", temp, hum);

	HAL_UART_Transmit(&huart2, tx_buffer, strlen((char *)tx_buffer), 60);
	printf("%s\r\n", tx_buffer);


	send_at_cmd((uint8_t*)"AT+MQTTPUB=0,\"$oc/devices/6426d4de0eafe83f11db2777_0001/sys/messages/up\",\"{\"services\": [{\"service_id\": \"1\",\"properties\": {\"CurrentTemperature\": 27.8}}]}\",0,0\r\n",(uint8_t*)"OK");
	HAL_Delay(6000);

	//led.led_off(led_b);


	//  printf("sht20_d:%d\r\n",SHT20_ReadData(SHT20_T_ADDR));

	  if( DS18B20_SampleData(&temperature) < 0)
	  {
		  printf("ERROR: DS18B20 Sample Data Failure\r\n");
	  }
	  else
	  {
		  printf("DS18B20 Sample Temperature: %.3f \r\n", temperature);
	  }

	  HAL_Delay(3000);
#endif

  }

  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 20;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
                              |RCC_PERIPHCLK_ADC;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
  PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_HSE;
  PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
  PeriphClkInit.PLLSAI1.PLLSAI1N = 9;
  PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;
  PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
  PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV6;
  PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_ADC1CLK;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

#if 0
int report_temp_json(void)
{
	char      buf[128];
	float     temperature;


	if( DS18B20_SampleData(&temperature) < 0)
	{
		printf("ERROR: DS18B20 Sample Data Failure\r\n");
	}

	memset(buf, 0, sizeof(buf));
	snprintf(buf, sizeof(buf), "{\"Tmeperature\": \"%.2f\"}\r\n", temperature);

	HAL_UART_Transmit(&huart1, (uint8_t *)buf, strlen(buf), 0xFFFF);

	return 0;
}

#endif


/* 串口数据接收完成回调函数 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == USART2)
	{
		HAL_UART_Receive_IT(&huart2, uart2.recv_buffer, uart2_recv_len);
	}
}


/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
